package subscription

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	businessOpportunityPb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
)

type subscriptionRepository struct {
}

// NewSubscriptionRepository 线索雷达订阅
func NewSubscriptionRepository() SubscriptionRepositoryIface {
	return &subscriptionRepository{}
}

func (r *subscriptionRepository) GetRadarSubscriptionConfig(ctx context.Context, request *businessOpportunityPb.GetRadarSubscriptionConfigRequest) (*businessOpportunityPb.GetRadarSubscriptionConfigResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetRadarSubscriptionConfig(ctx, request)
}

func (r *subscriptionRepository) SaveRadarSubscriptionConfig(ctx context.Context, request *businessOpportunityPb.SaveRadarSubscriptionConfigRequest) (*businessOpportunityPb.SaveRadarSubscriptionConfigResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.SaveRadarSubscriptionConfig(ctx, request)
}

func (r *subscriptionRepository) GetSubscriptionPushConfig(ctx context.Context, request *businessOpportunityPb.GetSubscriptionPushConfigRequest) (*businessOpportunityPb.GetSubscriptionPushConfigResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetSubscriptionPushConfig(ctx, request)
}

func (r *subscriptionRepository) SaveSubscriptionPushConfig(ctx context.Context, request *businessOpportunityPb.SaveSubscriptionPushConfigRequest) (*businessOpportunityPb.SaveSubscriptionPushConfigResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.SaveSubscriptionPushConfig(ctx, request)
}

func (r *subscriptionRepository) GetWxQrCode(ctx context.Context, request *businessOpportunityPb.GetWxQrCodeRequest) (*businessOpportunityPb.GetWxQrCodeResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetWxQrCode(ctx, request)
}

func (r *subscriptionRepository) BindOpenIdByCard(ctx context.Context, request *businessOpportunityPb.BindOpenIdByCardRequest) (*businessOpportunityPb.BindOpenIdByCardResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.BindOpenIdByCard(ctx, request)
}

func (r *subscriptionRepository) GetBindStatus(ctx context.Context, request *businessOpportunityPb.GetBindStatusRequest) (*businessOpportunityPb.GetBindStatusResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetBindStatus(ctx, request)
}

/*
func (r *subscriptionRepository) ApplyCron(ctx context.Context, params *subscriptionDto.EmptyRequest) (interface{}, error) {
	v := map[string]interface{}{}
	header := map[string]string{}
	paramsMap, err := simplecopier.StructToMapByJsonTag(params)
	paramsMap["id"] = ""
	if err != nil {
		stark.Logger.Errorf(ctx, "结构体转换map失败，err:%+v", err)
	}
	_, _, errs := http_client.NewGfyxModelApiHttpClient().Post(ctx, constants.GfyxApiPathApplyCron, paramsMap, header).EndStruct(&v)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	//处理建模接口的error
	errJm := http_client.NewGfyxModelApiHttpClient().GetJmError(v)
	if errJm != nil {
		return nil, errJm
	}

	return v, nil
}*/

func (r *subscriptionRepository) GetRadarList(ctx context.Context, request *businessOpportunityPb.GetRadarListRequest) (*businessOpportunityPb.GetRadarListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetRadarList(ctx, request)
}

func (r *subscriptionRepository) GetRadarPreCount(ctx context.Context, request *businessOpportunityPb.GetRadarPreCountRequest) (*businessOpportunityPb.GetRadarPreCountResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewSubscriptionServiceClient(conn)
	return client.GetRadarPreCount(ctx, request)
}
